<template>
	<view>
		<TopNav title="仓库信息"></TopNav>
		<u-form class="padding-lr-sm padding-bottom" labelWidth="88px" :model="form" ref="form" :rules="rules">
			<view class="padding-tb">
				<text class="cuIcon-title text-blue"></text>
				<text class="">基本信息</text>
			</view>
			<view class="padding-tb-xs padding-lr radius bg-white">

				<u-form-item label="仓库名称" required prop="name">
					<u-input v-model="form.name" border="none" placeholder="填写仓库名称" inputAlign="right"></u-input>
				</u-form-item>
			</view>
			<view class="padding-tb-xs padding-lr radius bg-white margin-top">
				<u-form-item label="备注" labelPosition="top">
					<u-textarea v-model="form.remarks"  border="bottom" placeholder="填写备注信息" autoHeight></u-textarea>
				</u-form-item>
			</view>
			<view class="padding-tb">
				<text class="cuIcon-title text-purple"></text>
				<text class="">相关图片</text>
			</view>
			<view class="padding radius bg-white">
				<UploadImage v-model="form.fileUrls" :limit="9"></UploadImage>
			</view>
		</u-form>
		<view class="bottom-tools padding flex justify-around bg-white">
			<van-button class='basis-xl' type="info" @click="handleSubmit('submit')">
				提交
			</van-button>
		</view>

	</view>
</template>

<script>
	import UploadImage from "@/components/upload-image.vue"
	import {
		detail,
		add,
		update
	} from "@/api/work/warehouse"
	export default {
		components: {
			UploadImage
		},
		data() {
			return {
				usageUnitDicts: [],
				productSourceDicts: [],
				queryParams: {
					id: undefined
				},
				form: {
					productionBaseId: undefined,
					name: undefined,
					remarks: undefined,
					fileUrls: undefined
				},
				rules: {
					name: {
						type: 'string',
						required: true,
						message: '仓库名称不能为空',
						trigger: ['blur', 'change']
					},

				}

			}
		},
		onReady() {
			//onReady 为uni-app支持的生命周期之一
			this.$refs.form.setRules(this.rules)
		},
		onLoad(query) {
			if (query.id) {
				this.queryParams.id = query.id
				this.getDetail()
			}
			this.form.productionBaseId = this.$store.state.user.baseInfo.id
		},
		onShow() {

		},
		methods: {
			handleSubmit(type) {
				this.$refs.form.validate().then(res => {
					if (this.form.id) {
						update(this.form).then(res => {
							this.$modal.msgSuccess('修改成功')
							uni.$emit('warehouseEdit')
							this.$tab.navigateBack()
						})
					} else {
						add(this.form).then(res => {
							this.$tab.navigateBack()
						})
					}
				})
			},
			getDetail() {
				detail(this.queryParams).then(res => {
					this.form = res.data
				})
			},


			reset() {
				this.form = {
					productionBaseId: undefined,
					name: undefined,
					remarks: undefined,
					fileUrls: undefined
				}
				this.$nextTick(() => {
					this.$refs.form.resetFields()
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "@/common/var.scss";
	page {
		@include fixed-bottom;
	}
	
</style>